import { MindGraph } from './MindGraph'
import { ShorcutOptions} from './models'
import * as MGUtils from './utils'
import { Mind } from './Mind'

export class ShorcutProvider {
    mindGraph: MindGraph = null

    opts: ShorcutOptions = null

    mapping = this.opts?.mapping
    
    handles = this.opts?.handles // 以 ‘作用名' 为key， 以 处理函数 为值

    _mapping = {}

    constructor(mindGraph: MindGraph, options: ShorcutOptions) {
        this.mindGraph = mindGraph
        this.opts = options
        this.mapping = this.opts.mapping
        this.handles = this.opts.handles
        this._mapping = {}
    }

    /**
     * 快捷键初始化
     */
    init() {
        MGUtils.DomUtils.addEvent(document,'keydown',this.handler.bind(this));

            this.handles['addchild'] = this.handleAddChild;
            this.handles['addbrother'] = this.handleAddBrother;
            this.handles['editnode'] = this.handleEditNode;
            this.handles['delnode'] = this.handleDelNode;
            this.handles['toggle'] = this.handleToggle;
            this.handles['up'] = this.handleUp;
            this.handles['down'] = this.handleDown;
            this.handles['left'] = this.handleLeft;
            this.handles['right'] = this.handleRight;

            for(var handle in this.mapping){
                if(!!this.mapping[handle] && (handle in this.handles)){
                    this._mapping[this.mapping[handle]] = this.handles[handle];
                }
            }

    }

    /**
     * TODO 
     */
    handler (e) {
        if(this.mindGraph.view.isEditing()){return;}
        var evt = e || event;
        if(!this.opts.enable){return true;}
        var kc = evt.keyCode;
        if(kc in this._mapping){
            this._mapping[kc].call(this,this.mindGraph,e);
        }
    }

    handleAddChild (mindGraph: MindGraph, e) {

    }
    handleAddBrother (mindGraph: MindGraph, e) {

    }
    handleEditNode (mindGraph: MindGraph, e) {

    }
    handleDelNode (mindGraph: MindGraph, e) {

    }
    handleToggle (mindGraph: MindGraph, e) {

    }
    handleUp (mindGraph: MindGraph, e) {

    }
    handleDown (mindGraph: MindGraph, e) {

    }
    handleLeft (mindGraph: MindGraph, e) {

    }
    handleRight (mindGraph: MindGraph, e) {

    }

}